package cn.lanqiao.book.controller;

import cn.lanqiao.book.dao.IAdminDao;
import cn.lanqiao.book.dao.impl.AdminDaoImplI;
import cn.lanqiao.book.pojo.Admin;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AdminServlet extends HttpServlet {
    Admin ad = new Admin();
    IAdminDao add = new AdminDaoImplI();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("_method");
        try {
            Method m = getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void addAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 3) {
                String user = req.getParameter("u_username");

                String password = req.getParameter("u_password");
                String email = req.getParameter("u_email");
                int sex;
                if (req.getParameter("u_sex") == "") {
                    sex = 3;
                } else {
                    sex = Integer.parseInt(req.getParameter("u_sex"));
                }
                String qq;
                if (req.getParameter("u_qq") == "") {
                    qq = null;
                } else {
                    qq = req.getParameter("u_qq");
                }

                if (user == null || password == null || email == null) {
                    out.println("账号密码邮箱为必填");
                    return;
                } else {
                    if (sex != 1 || sex != 2 || sex != 3) {
                        sex = 3;
                    }
                    int type2 = 2;
                    ad.setU_username(user);
                    ad.setU_password(password);
                    ad.setU_email(email);
                    ad.setU_sex(sex);
                    ad.setU_qq(qq);
                    ad.setU_type(type2);
                    add.addAdmin(ad);
                    req.getRequestDispatcher("/mutilAdmin?_method=selectAdmin").forward(req, resp);
                }
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void deleteAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || name1.equals(name2)) {
                String name = req.getParameter("u_username");
                add.deleteAdmin(name);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
            req.getRequestDispatcher("/admin.jsp").forward(req, resp);
        }
    }

    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("u_username");
                add.deleteAdmin(name);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
            req.getRequestDispatcher("/user.jsp").forward(req, resp);
        }
    }

    public void deleteUser1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("u_username");
                add.deleteAdmin(name);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void deleteAdmin1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || name1.equals(name2)) {
                String name = req.getParameter("u_username");
                add.deleteAdmin(name);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html; charset=UTF-8");
        HttpSession session = req.getSession();
        String name = req.getParameter("username");
        String password = req.getParameter("password");
        ad = add.queryForOne(name);
        if (ad == null) {
            resp.getWriter().write("账号不存在");
        } else {
            if (!password.equals(ad.getU_password())) {
                resp.getWriter().write("密码错误");
            } else if (password != null) {
                session.setAttribute("username", name);
                session.setAttribute("password", ad.getU_password());
                session.setAttribute("type", ad.getU_type());
                String a = Integer.toString(ad.getU_type());
                Map<String, Object> map = new HashMap();
                map.put("type", a);
                map.put("username", name.length() > 6 ? name.substring(0, 5) + "..." : name);
                String toString = JSON.toJSONString(map);
                resp.getWriter().write(toString);
            }
        }
    }

    public void selectAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Object type = session.getAttribute("type");

        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 2 || type1 == 3) {
                List<Admin> l = new ArrayList<>();
                List<Admin> l2 = new ArrayList<>();
                l = add.queryAll();
                for (Admin admin : l) {
                    if (admin.getU_type() != 1) {
                        l2.add(admin);
                    }
                }
                req.setAttribute("admin", l2);
                req.getRequestDispatcher("/admin.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void selectUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Object type = session.getAttribute("type");

        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 2 || type1 == 3) {
                List<Admin> l = new ArrayList<>();
                List<Admin> l2 = new ArrayList<>();
                l = add.queryAll();
                for (Admin admin : l) {
                    if (admin.getU_type() == 1) {
                        l2.add(admin);
                    }
                }
                req.setAttribute("admin", l2);
                req.getRequestDispatcher("/user.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void searchAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("u_username");
        name = "%" + name + "%";
        List<Admin> l = new ArrayList<>();
        List<Admin> l2 = new ArrayList<>();
        l = add.search(name);
        for (Admin admin : l) {
            if (admin.getU_type() != 1) {
                l2.add(admin);
            }
        }
        req.setAttribute("admin", l2);
        req.getRequestDispatcher("/admin.jsp").forward(req, resp);
    }

    public void searchUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("u_username");
        name = "%" + name + "%";
        List<Admin> l = new ArrayList<>();
        List<Admin> l2 = new ArrayList<>();
        l = add.search(name);
        for (Admin admin : l) {
            if (admin.getU_type() == 1) {
                l2.add(admin);
            }
        }
        req.setAttribute("admin", l2);
        req.getRequestDispatcher("/user.jsp").forward(req, resp);
    }


    public void qurryAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();

        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || name1.equals(name2)) {
                ad = add.queryForOne(name2);
                req.setAttribute("admin", ad);
                req.getRequestDispatcher("/updateadmin.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void qurryUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();

        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            String name1 = (String) session.getAttribute("username");
            String name2 = req.getParameter("u_username");
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("u_username");
                ad = add.queryForOne(name);
                req.setAttribute("admin", ad);
                req.getRequestDispatcher("/updateuser.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String user = req.getParameter("u_username");
        String password = req.getParameter("u_password");
        String email = req.getParameter("u_email");
        int sex = Integer.parseInt(req.getParameter("u_sex"));
        String qq = req.getParameter("u_qq");
        int id = Integer.parseInt(req.getParameter("u_id"));

        if (qq == "") {
            qq = null;
        }

        ad.setU_username(user);
        ad.setU_password(password);
        ad.setU_email(email);
        ad.setU_sex(sex);
        ad.setU_qq(qq);
        ad.setU_id(id);

        add.updateAdmin(ad);
        req.getRequestDispatcher("/mutilAdmin?_method=selectAdmin").forward(req, resp);
    }

    public void updateUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String user = req.getParameter("u_username");
        String password = req.getParameter("u_password");
        String email = req.getParameter("u_email");
        int sex = Integer.parseInt(req.getParameter("u_sex"));
        String qq = req.getParameter("u_qq");

        if (qq == "") {
            qq = null;
        }

        int id = Integer.parseInt(req.getParameter("u_id"));


        ad.setU_username(user);
        ad.setU_password(password);
        ad.setU_email(email);
        ad.setU_sex(sex);
        ad.setU_qq(qq);
        ad.setU_id(id);

        add.updateAdmin(ad);
        req.getRequestDispatcher("/mutilAdmin?_method=selectUser").forward(req, resp);
    }

    public void changerPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String user = req.getParameter("u_username");
        String password = req.getParameter("u_password");
        String newpassword = req.getParameter("u_newpassword");

        ad = add.queryForOne(user);
        if (ad == null) {
            out.println("用户名错误");
        } else {
            String oldP = ad.getU_password();
            if (!oldP.equals(password)) {
                out.println("原密码输入错误");
            } else {
                int id = ad.getU_id();
                add.changerPassword(id, newpassword);
            }
        }
        req.getRequestDispatcher("/admin.jsp").forward(req, resp);
    }

    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();
        session.removeAttribute("username");
        session.removeAttribute("password");
        session.removeAttribute("type");
        //使session失效
        session.invalidate();
        resp.getWriter().write("成功安全退出");
//        resp.sendRedirect("/index.jsp");
    }

    public void checkname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String user = req.getParameter("u_username");
        System.out.println(user);
        Admin admin;
         admin = add.checkname(user);
        int flag=2;
        if (null == admin){
            //无重复，可以插入
            flag =1;
            System.out.println(flag);
            resp.getWriter().write(""+flag);
        }else{
            //重复，不可以插入
            System.out.println(flag);
            resp.getWriter().write(""+flag);
        }
    }
    public void checkemail(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String email = req.getParameter("u_email");
        Admin admin = add.checkemail(email);
        int flag=2;
        if (null == admin){
            //无重复，可以插入
            flag =1;
            System.out.println(flag);
            resp.getWriter().write(""+flag);
        }else{
            //重复，不可以插入
            System.out.println(flag);
            resp.getWriter().write(""+flag);
        }
    }
}
